package com.tecklab.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tecklab.demo.aspect.OperateLog;
import com.tecklab.demo.common.PageData;
import com.tecklab.demo.common.Result;
import com.tecklab.demo.dto.HospitalDTO;
import com.tecklab.demo.entity.HospitalEntity;
import com.tecklab.demo.mapper.HospitalMapper;
import com.tecklab.demo.service.HospitalService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 示例Controller类
 * 提供基本的REST API端点
 */
@RestController
@RequestMapping("/api/hospital")
public class HospitalController {

    @Resource
    private HospitalService hospitalService;

    @Resource
    private HospitalMapper hospitalMapper;


    @GetMapping
    @OperateLog(optName = "分页查询医院", optType = "查询")
    public Result getPage(@RequestParam("page") Integer pageNum, @RequestParam Integer size,
                          @RequestParam(required = false) String name) {
        Page<HospitalEntity> page = new Page<>(pageNum, size);
        QueryWrapper<HospitalEntity> queryWrapper = new QueryWrapper<>();
        if (name != null && !name.isEmpty()) {
            queryWrapper.like("name", name);
        }
        page = hospitalMapper.selectPage(page, queryWrapper);
        return Result.success(new PageData<>(page.getRecords(), page.getTotal()));
    }

    @PostMapping
    @OperateLog(optName = "添加医院", optType = "新增")
    public Result save(@RequestBody HospitalDTO dto) {
         hospitalService.save(dto);
        return Result.success();
    }

    @PutMapping("/{id}")
    @OperateLog(optName = "更新医院", optType = "修改")
    //updateUser--updateHospital
    public Result updateHospital(@PathVariable Integer id, @RequestBody HospitalEntity hospital) {
        hospital.setId(id);
        hospitalMapper.updateById(hospital);
        return Result.success();
    }

    @GetMapping("/batch/create")
    @OperateLog(optName = "批量添加医院", optType = "新增")
    public Result<Void> batchCreate(@RequestParam Integer size) {
         hospitalService.batchCreateHospital(size);
         return Result.success();
    }

    @DeleteMapping("/{id}")
    @OperateLog(optName = "删除医院", optType = "删除")
    public Result delete(@PathVariable Integer id) {
        hospitalMapper.deleteById(id);
        return Result.success();
    }

    @PostMapping("/batch/delete")
    @OperateLog(optName = "批量删除医院", optType = "删除")
    public Result batchDeleteUser(@RequestBody List<Integer> idList) {
        hospitalMapper.deleteBatchIds(idList);
        return Result.success();
    }
}
